package net.sxnic.ugr.role.action;

import net.sxinfo.core.entity.EntityAlreadyExistsException;
import net.sxnic.ugr.UgrActionSupport;
import net.sxnic.ugr.menu.Menu;
import net.sxnic.ugr.role.Role;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@SuppressWarnings("serial")
public class Create extends UgrActionSupport {
	
	private static Logger logger = LoggerFactory.getLogger(Create.class);

    private Role role;
    
    /**
	 * 菜单
	 */
	private String menu;
	
	

    public String execute() throws Exception {
       

        try {
        	
        	 checkRoles(Role.SYSTEM_ADMIN);
        	  		 
    			// 角色
    			if (StringUtils.isBlank(menu)) {
    				role.getMenus().clear();
    				 
    			} else {
    				 
    				String[] menus = StringUtils.split(menu, ",");
    				role.getMenus().clear();
    				Menu m = null;
    				for (String rr : menus) {
    					m = authorizationManager.getMenu (rr.trim());
    					logger.debug("menu id :" + m.getId() + " , role id :"
    							+ role.getId());

    					role.getMenus().add(m);
    					 
    				}
    				//user.setRoleCodes(StringUtils.removeEnd(tempCodes, ","));
    			} 

    		 
    	    this.authorizationManager.createRole(role);
            authorizationManager.updateRole(role);
         // 保存成功信息并转向
            msg="createSuccess";

        } catch (EntityAlreadyExistsException eaee) {
//            addActionError(getText("security.error.roleExists",
//                new String[] { role.getRole() }));

            return INPUT;
        }

        

        return SUCCESS;
    }

    /**
     * @return the role
     */
    public Role getRole() {
        return role;
    }

    /**
     * @param role the role to set
     */
    public void setRole(Role role) {
        this.role = role;
    }

	public String getMenu() {
		return menu;
	}

	public void setMenu(String menu) {
		this.menu = menu;
	}
    
}
